<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;

class Hsimages extends AdminBase
{
    public $class_type;
    protected function _initialize()
    {
        parent::_initialize();
        //图片文件夹、模型名称、页面显示名称、需要查询的字段
        $this->class_type = [
            'banner'=>['model'=>'ads','title'=>'轮播图片','field'=>'img_url_cn|img_url_en'],
            'nav' => ['model'=>'nav','title'=>'导航图片','field'=>'img_url_cn|img_url_en'],
            'listinfo' => ['model'=>'listinfo','title'=>'列表信息图片','field'=>'img_url_cn|img_url_en'],
            'pageinfo' => ['model'=>'pageinfo','title'=>'单页信息图片','field'=>'img_url_cn|img_url_en','like'=>'json_arr_cn|json_arr_en'],
            //'link'=>['model'=>'link','title'=>'友链图片','field'=>'img_url'],
        ];
        $this->assign('class_type', $this->class_type);
    }

    public function index()
    {
        $type = input('type',key($this->class_type));
        $m = model($this->class_type[$type]['model']);
        $images = $this->byDirAddFile('./uploads/'.$type);
        if($images){
            foreach ($images as $k=>$v){
                $img = ltrim($v,'.');
                //->withTrashed(),查询包括软删除的数据
                $exist1 = false;    $exist2 = false;
                if(isset($this->class_type[$type]['like'])){    //json化的数据为\/,addcslashes,反斜线在查询时会转义,故这里只查询文件名称（无重复）,不带文件夹
                    $exist1 = $m->where($this->class_type[$type]['like'],'like','%'.substr($img,strripos($img,"/")+1).'%')->count()>0 ? true : false;
                }
                if(isset($this->class_type[$type]['field'])){
                    $exist2 = $m->where($this->class_type[$type]['field'],'=',$img)->count()>0 ? true : false;
                }
                $exist = ($exist2 || $exist1) ? true : false;
                $_images[] = ['img'=>$img,'exist'=>$exist];
            }

            $_images = array_chunk($_images,4);
        }
        return $this->fetch('index',['list'=>$_images]);
    }

    public function delete($img,$type){
        if($this->request->isAjax()){
            if($img && $type){
                $m = model($this->class_type[$type]['model']);

                $exist1 = false;    $exist2 = false;
                if(isset($this->class_type[$type]['like'])){    //json化的数据为\/,addcslashes,反斜线在查询时会转义,故这里只查询文件名称（无重复）,不带文件夹
                    $exist1 = $m->where($this->class_type[$type]['like'],'like','%'.substr($img,strripos($img,"/")+1).'%')->count()>0 ? true : false;
                }
                if(isset($this->class_type[$type]['field'])){
                    $exist2 = $m->where($this->class_type[$type]['field'],'=',$img)->count()>0 ? true : false;
                }
                $exist = ($exist2 || $exist1) ? true : false;

                //$exist = $m->withTrashed()->where($this->class_type[$type]['field'],'=',$img)->count()>0 ? true : false;
                if($exist) $this->error('当前图片正在使用中，不可删除');
                if(@unlink('.'.$img)){
                    $this->success('删除成功');
                }else{
                    $this->error('删除失败');
                }
            }else{
                $this->error('参数异常');
            }
        }
    }

    /**
     * 获取文件夹下所有文件
     * @param string $dir  相对目录
     * @param null $out
     * @return array|null
     */
    public function byDirAddFile($dir, &$out = null){
        $out == null && $out = array();
        if (is_dir($dir) && ($dh = opendir($dir))) {
            while (($file = readdir($dh)) !== false) {
                if($file == '.' || $file == '..') {
                    continue;
                }
                if(file_exists($dir. '/'. $file) && !is_dir($dir. '/'. $file)){// && !is_dir($dir. '/'. $file)子目录下文件一并添加到out,不包含目录元素
                        $out[] = $dir. '/'. $file;  //包含路径
                        //$file=strstr ( $file , '.' , true );//过滤文件后缀
                        //$out[] = $file;    //不包路径
                }else{
                    $this->byDirAddFile($dir. '/'. $file, $out);
                }
            }
            closedir($dh);
        }
        return $out;
    }
}